package com.showcai.controller.order;

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.module.Order;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.redis.RedisCache;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ApplyService;
import com.showcai.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-1-25.
 */
@RequestMapping("/order")
@Controller
public class OrderController extends WeChatBaseController {
    @Autowired
    OrderService orderService;
    @Autowired
    ApplyService applyWatchService;
    @Autowired
    RedisCache redisCache;

    @RequestMapping(value="/selectInfo")
    @ResponseBody
    public String selectInfo(String phone,String code) {
        ResultObj resultObj = new ResultObj();
        try {
            String ingfo = (String)redisCache.get(phone+"_snedInfo");
            if(ingfo.equals(code)){
                resultObj.setCode(ReturnCode.SUCCESS.getCode());
                resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
            }else{
                resultObj.setCode(ReturnCode.FAIL.getCode());
                resultObj.setMsg("验证码不正确");
            }
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg("验证码过期！");
        }
        return resultObj.toJson();
    }


    /**
     * 查询页面
     * @return
     * https://www.showcai.com/order/orderQuery.html
     * queryType=1 跳转到退款
     * 其他跳转到订单查询
     */
    @RequestMapping(value="/orderQuery")
    public ModelAndView orderQuery(Byte queryType) {
        ModelAndView mov=new ModelAndView();
        String views="order/web/orderQuery";
        if(getIsMobile()){
            views="order/wap/orderQuery";
        }

        mov.setViewName(views);
        mov.addObject("queryType",queryType);
        return mov;
    }

    /**
     * 订单列表
     * @return
     */
    @RequestMapping(value="/orderList")
    public ModelAndView orderList(String phone) {
        ModelAndView mov=new ModelAndView();
        String views="order/web/orderList";
        if(getIsMobile()){
            views="order/wap/orderList";
        }
        Map<String,String> map=new HashMap<>();
        map.put("phone",phone);
        List<Order> orderList =  orderService.getOrderList(map);
        mov.addObject("orderList",orderList);
        mov.setViewName(views);
        return mov;
    }
    /**
     * 退款订单列表
     * @return
     */
    @RequestMapping(value="/refundOrderList")
    public ModelAndView refundOrderList(String phone) {
        ModelAndView mov=new ModelAndView();
        String views="order/web/refundOrderList";
        if(getIsMobile()){
            views="order/wap/refundOrderList";
        }
        Map<String,String> map=new HashMap<>();
        map.put("phone",phone);
         List<Order> orderList =  orderService.getOrderList(map);
        mov.addObject("orderList",orderList);
        mov.setViewName(views);
        return mov;
    }


    @RequestMapping(value="/getOrderByPhone")
    @ResponseBody
    public String getOrderByPhone(String phone) {
        ResultObj resultObj=new ResultObj();
        try {
            Map<String,String> map=new HashMap<>();
            map.put("phone",phone);
            List<Order> orderList =  orderService.getOrderList(map);
            resultObj.setData(orderList);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        }
        return  resultObj.toJson();
    }


    @RequestMapping(value="/createOrder")
    @ResponseBody
    public String createOrder(String productEntityIds,String activityIds, Byte orderSourceId) {
        ResultObj resultObj=new ResultObj();
        try {
            User user=super.getUserBySession();
            List<Long> productEntityIdList=new ArrayList<>();
            for (String productEntityId:productEntityIds.split(",")
                 ) {
                productEntityIdList.add(Long.parseLong(productEntityId));
            }
            List<Long> activityIdList=new ArrayList<>();
            for (String activityId:activityIds.split(",")
                    ) {
                activityIdList.add(Long.parseLong(activityId));
            }
            logger.info(activityIds);
         Order order=   orderService.createOrder(productEntityIdList,user.getTid(),activityIdList,orderSourceId);
            resultObj.setData(order);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        }
        return  resultObj.toJson();
    }
}
